<?php

    function latest_news($news_num)
    {
        $list = api_news::get_all_latest_news($news_num);
        if (empty($list) || count($list) == 0) {
            echo "Chưa có bài báo nào.";
        } else {
            foreach ($list as $item) {
                ?>
                <div class="ts-latest-news-item">
                    <table>
                        <tr>
                            <td>
                                <a href="news_detail.php?news_id=<?php echo $item['news_id']; ?>">
                                    <img src="<?php echo LINK_ROOT . DIR_SHARED_LAYOUT_IMAGES_ICONS . 'news.png' ?>"
                                         alt="News"/>
                                </a>
                            </td>
                            <td rowspan="2">
                                <div class='ts-item-title'><a
                                        href="news_detail.php?news_id=<?php echo $item['news_id']; ?>"><?php echo $item['news_title']; ?></a>
                                </div>
                                <?php
                                $date = date_create($item["news_added_date"]);
                                $date_str = date_format($date, "d/m/Y H:i");
                                ?>
                                <div class='ts-item-date'>Ngày gửi: <?php echo $date_str; ?></div>
                            </td>
                        </tr>
                    </table>
                </div>
            <?php
            }
        }
    }

    function show_news_category_group($category)
    {
        $count_total_newses = api_news::count_news_by_category_id($category['cat_news_id']);
        $newses             = api_news::get_all_news_by_category_id($category['cat_news_id']);
        $list = $newses;

        ?>
        <div class="uk-panel uk-margin-bottom">
            <h3 class="ts-category-title">
                <a href="<?php echo LINK_ROOT ?>/news.php" data-uk-tooltip
                   title="Xem tin tức thuộc <b style='color: #fff;'><?php echo $category['cate_news_name']; ?></b>">
                    <i class="uk-icon-folder-open"></i> <?php echo $category['cate_news_name']; ?>
                    <span>(<?php echo $count_total_newses; ?> tin)</span>
                </a>
            </h3>

            <div class="ts-category-content">
                <?php
                    if (empty($list) || count($list) == 0) {
                        echo "Chưa có tin tức nào";
                    } else {
                        foreach ($list as $item) {
                            ?>
                            <div class="ts-item">
                                <table>
                                    <tr>
                                        <td>
                                            <a href="news_detail.php?news_id=<?php echo $item['news_id']; ?>">
                                                <img
                                                    src="<?php echo LINK_ROOT . DIR_SHARED_LAYOUT_IMAGES_ICONS . 'news.png' ?>"
                                                    alt="News"/>
                                            </a>
                                        </td>
                                        <td rowspan="2">
                                            <div class='ts-item-title'><a
                                                    href="news_detail.php?news_id=<?php echo $item['news_id']; ?>"><?php echo $item['news_title'] ?></a>
                                            </div>
                                            <?php
                                            $date = date_create($item["news_added_date"]);
                                            $date_str = date_format($date, "d/m/Y H:i");
                                            ?>
                                            <div class='ts-item-date'>Ngày
                                                gửi: <?php echo $date_str; ?></div>
                                        </td>
                                    </tr>
                                </table>
                            </div>
                        <?php
                        }
                    }
                ?>


            </div>
        </div>
    <?php
    }

    function show_category_group($category, $maximum_docs_show)
    {
        $count_total_docs  = api_document::count_document_by_category_id($category['cat_id']);
        $documents         = api_document::get_all_documents_by_category_id($category['cat_id']);
        $list              = array();
        if (count($documents) < $maximum_docs_show || $maximum_docs_show == -1) {
            $list = $documents;
        } else {
            $random_keys = array_rand($documents, $maximum_docs_show);
            for ($index = 0; $index < $maximum_docs_show; $index++) {
                $list[count($list)] = $documents[$random_keys[$index]];
            }
        }
        ?>
        <div class="uk-panel uk-margin-bottom">
            <h3 class="ts-category-title">
                <a href="<?php echo LINK_ROOT ?>/document.php?cat_id=<?php echo $category['cat_id']; ?>" data-uk-tooltip
                   title="Xem bài tập thuộc <b style='color: #fff;'><?php echo $category['cat_name']; ?></b>">
                    <i class="uk-icon-folder-open"></i> <?php echo $category['cat_name']; ?>
                    <span>(<?php echo $count_total_docs; ?> bài)</span>
                </a>
            </h3>
            <?php
                show_content_group($list);
            ?>


        </div>
    <?php
    }

    function show_content_group($list)
    {
        ?>
        <div class="ts-category-content">
            <?php
                if (empty($list) || count($list) == 0) {
                    echo "Không có bài tập nào.";
                } else {
                    foreach ($list as $item) {
                        $doc_img = "doc.png";
                        if (!empty($item['doc_img_url'])) {
                            $doc_img = $item['doc_img_url'];
                        }
                        ?>
                        <div class="ts-item">
                            <table>
                                <tr>
                                    <td>
                                        <a href="document_detail.php?doc_id=<?php echo $item['doc_id']; ?>">
                                            <img
                                                src="<?php echo LINK_ROOT . DIR_SHARED_UPLOAD_IMAGES_DOCUMENT . $doc_img; ?>"
                                                alt="Document"/>
                                        </a>
                                    </td>
                                    <td rowspan="2">
                                        <div class='ts-item-title'><a
                                                href="document_detail.php?doc_id=<?php echo $item['doc_id']; ?>"><?php echo $item['doc_name'] ?></a>
                                        </div>
                                        <?php
                                        $date = date_create($item["doc_added_date"]);
                                        $date_str = date_format($date, "d/m/Y H:i");
                                        ?>
                                        <div class='ts-item-date'>Ngày gửi: <?php echo $date_str; ?></div>
                                    </td>
                                </tr>
                            </table>
                        </div>
                    <?php
                    }
                }
            ?>
        </div>
    <?php
    }

    function show_root_list()
    {
        $root_list = api_multi_level::get_root_document_categories();

        if (empty($root_list) || count($root_list) == 0) {
            echo "Chưa có danh mục nào.";
        } else {
            foreach ($root_list as $root_item) {
                ?>
                <li onclick="filldata('<?php echo $root_item['cat_id']; ?>');">
                    <a href="javascript:void(0)"><i class="uk-icon-plus"></i></a>
                    <a href="document.php?cat_id=<?php echo $root_item['cat_id']; ?>"><i
                            class="uk-icon-folder"></i> <?php echo $root_item['cat_name']; ?></a>
                </li>
            <?php
            }
        }
    }

    function show_news_root_list()
    {
        $root_list = api_multi_level::get_root_news_categories();
        if (empty($root_list) || count($root_list) == 0) {
            echo "Chưa có danh mục nào.";
        } else {
            foreach ($root_list as $root_item) {
                ?>
                <li onclick="filldata('<?php echo $root_item['cat_news_id']; ?>');">
                    <a href="javascript:void(0)"><i class="uk-icon-plus"></i></a>
                    <a href="news.php?cat_id=<?php echo $root_item['cat_news_id']; ?>"><i
                            class="uk-icon-folder"></i> <?php echo $root_item['cate_news_name']; ?></a>
                </li>
            <?php
            }
        }
    }